<?php 
$output = 'var tinyMCETemplateList = new Array(';
$delimiter = "\n";
//$directory = "../../upload/templates"; // Use your correct (relative!) path here
//$directory = "../../../../uploads/templates"; // Use your correct (relative!) path here
$directory = $_SERVER['DOCUMENT_ROOT'] . "/uploads/templates"; // Use your correct (relative!) path here
$url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on'? 'https://': 'http://';
$url .= $_SERVER['SERVER_NAME'].'/uploads/templates';

$abspath = preg_replace('~^/?(.*)/[^/]+$~', '/$1', $_SERVER['SCRIPT_NAME']); // Since TinyMCE3.x you need absolute image paths

$TinyMceTemplateExts = array( // allowed extensions
    'tpl'=>'1', 'tmpl'=>'1', 'htm'=>'1', 'html'=>'1'
);

if(is_dir($directory))
{   if($direc = opendir($directory)){}else{}//end if else
    while($file = readdir($direc))
	{	//if(!preg_match('~^.~', $file))// no hidden files / directories here...
		if(preg_match('~^.~', $file))// no hidden files / directories here...
		{	if(is_file("{$directory}/{$file}")) 
			{	preg_match('/\.([^.]+)$/',$file,$match);
                if($TinyMceTemplateExts[@$match[1]])
				{	$output .= $delimiter
                    		. '["'.utf8_encode($file).'", "'.utf8_encode($url . "/{$file}").'"],';
               	}//end if
            }else{}//end if
        }else{}//end if
    }//end while loop
    $output = substr($output, 0, -1); // remove last comma from array item list (breaks some browsers)
    $output .= $delimiter;
    closedir($direc);
}else{}//end if
$output .= ');';

header('Content-type: text/javascript'); // browser will now recognize the file as a valid JS file
header('pragma: no-cache');	// prevent browser from caching
header('expires: 0'); // i.e. contents have already expired
echo $output;
?>